home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / tc-pull.lzh / TC_ASM.C next >
Encoding:
C/C++ Source or Header  |  1987-11-26  |  20.0 KB  |  517 lines

  1. /* ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ */
  2. /* ▌ tc_asm.c                                          ▐ */
  3. /* ▌                                                   ▐ */
  4. /* ▌ to compile:   tcc -ms -c tc_asm                   ▐ */
  5. /* ▌                   -ms = Small Model               ▐ */
  6. /* ▌                         Change if Desired         ▐ */
  7. /* ▌                         (note #define LARGE)      ▐ */
  8. /* ▌                                                   ▐ */
  9. /* ▌             requires MASM for the inline assembly ▐ */
  10. /* ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ */
  11.  
  12. #pragma inline
  13.  
  14.  
  15. #define LARGE  0    /* 0 for SMALL model           */
  16. /* change to 1 for LARGE model */
  17.  
  18.  
  19.  
  20. /*  ╔══  FUNCTION DECLARATIONS ═══════════════════════╗  */
  21.  
  22. void fastwrite(int, int, int, char *);    
  23. void getscrn(char far*);
  24. void dma_ca(int, int, int, int);    
  25. void putscrn(char far*);
  26. void dma_str(char *, int);
  27. void scn_attr(int,int,int,int);
  28. void set_drive(int);                          
  29. int vpeek(unsigned , unsigned);
  30. void vpoke(unsigned, unsigned, unsigned);
  31.  
  32. /*  ╚══  FUNCTION DECLARATIONS ═══════════════════════╝  */
  33.  
  34.  
  35.  
  36. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  37. /*  ║ Function :  fastwrite();                                             ║  */
  38. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  39. /*  ║ direct screen write of character and attribute with snow check       ║  */
  40. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  41. /*  ║ Usage    : fastwrite(col,row,attr,string)                            ║  */
  42. /*  ║                                                                      ║  */
  43. /*  ║ Returns  : nothing                                                   ║  */
  44. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  45. void fastwrite(int col, int row, int attr, char *st)
  46. {
  47.      asm  mov    bx,si           /* save SI in BX                      */
  48.      asm  mov    ax,40h          /* get video mode                     */
  49.      asm  mov    es,ax
  50.      asm  mov    dl,es:[49h]     /* DL=Video mode, stored by BIOS      */
  51.      asm  mov    di,row          /* DI=Row                             */
  52.      asm  mov    cl,4            /* CL=4, CH=0                         */
  53.      asm  shl    di,cl           /* DI=Row*32                          */
  54.      asm  mov    ax,di           /* store in AX                        */
  55.      asm  shl    di,1            /* DI=Row*32                          */
  56.      asm  shl    di,1            /* DI=Row*64                          */
  57.      asm  add    di,ax           /* di=row*80                          */
  58.      asm  add    di,col          /* Add (Col +1) to di                 */
  59.      asm  shl    di,1            /* account for attribute byte         */
  60.      #if LARGE
  61.      asm  lds    si,st           /* DS:SI points to String             */
  62.      # else
  63.      asm  mov    si,st           /* DS:SI points to String             */
  64.      #endif
  65.      asm  mov    ah,attr         /* AH = attribute                     */
  66.      asm  cmp    dl,7            /* Mono??                             */
  67.      asm  je     mono
  68.      asm  mov    dx,0b800h       /* base of video buffer               */
  69.      asm  mov    es,dx           /* ES = segment for color memory      */
  70.      asm  mov    dx,03dah        /* 6845 port address                  */
  71.      
  72.      getnext:
  73.      asm  lodsb      /* load next char into al             */
  74.      asm  or     al,al           /* test for null character            */
  75.      asm  jz     exit            /* exit if it's a null                */
  76.      asm  mov    cx,ax           /* store video word in CX             */
  77.      asm  cli                    /* no interrupts                      */
  78.      
  79.      waitnoh:
  80.      asm  in     al,dx        /* get 6845 status           */
  81.      asm  test   al,8            /* Vertical retrace?                  */
  82.      asm  jnz    store           /* if so, go                          */
  83.      asm  rcr    al,1            /* else, wait for end of              */
  84.      asm  jc     waitnoh         /* horizontal retrace                 */
  85.      
  86.      waith:
  87.      asm  in     al,dx        /* get 6845 status again     */
  88.      asm  rcr    al,1            /* wait for horizontal                */
  89.      asm  jnc    waith           /* retrace                            */
  90.      
  91.      store:
  92.      asm  mov    ax,cx        /* move word back to AX..    */
  93.      asm  stosw                  /* and then to screen                 */
  94.      asm  sti                    /* allow interrupts                   */
  95.      asm  jmp    short getnext   /* get next character                 */
  96.      
  97.      mono:
  98.      asm  mov    dx,0b000h
  99.      asm  mov    es,dx           /* ES = segment for mono memory       */
  100.      
  101.      next:
  102.      asm  lodsb      /* Load next char into AL             */
  103.      asm  or     al,al           /* Test for null char                 */
  104.      asm  jz     exit            /* Exit if it's a null                */
  105.      asm  stosw                  /* Move video word into place         */
  106.      asm  jmp    short next      /* get next character                 */
  107.      
  108.      exit:
  109.      asm  mov    si,bx        /* restore SI from BX        */
  110. }
  111.  
  112.  
  113. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  114. /*  ║ Function :  getscrn();                                               ║  */
  115. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  116. /*  ║ puts screen to memory buffer using direct screen writes w/snowcheck  ║  */
  117. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  118. /*  ║ Usage    : getscrn(buff);                                            ║  */
  119. /*  ║                                                                      ║  */
  120. /*  ║ Returns  : nothing                                                   ║  */
  121. /*  ║                                                                      ║  */
  122. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  123. /*  ║      ex:  static char  buff[4000];                                   ║  */
  124. /*  ║                                                                      ║  */
  125. /*  ║            getscrn(buff);    SAVE SCREEN                             ║  */
  126. /*  ║            other_stuff();    DO SOME STUFF                           ║  */
  127. /*  ║            putscrn(buff);    RESTORE SCREEN                          ║  */
  128. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  129. void getscrn(buff)
  130. char far *buff;
  131. {
  132.      asm  push    ds
  133.      asm  push    es
  134.      
  135.      asm  mov     ah,15
  136.      asm  int     10h 
  137.      asm  mov     dx,3BAh
  138.      asm  mov     si,0  
  139.      asm  cmp     al,7 
  140.      asm  je      movscn
  141.      asm  mov     dx,3DAh 
  142.      asm  mov     si,8000h
  143.      
  144.      movscn:
  145.      asm  mov     ax,0B000h
  146.      asm  push    ds
  147.      asm  pop     es
  148.      asm  mov     ds,ax
  149.      
  150.      #if LARGE 
  151.      asm  les     di,buff
  152.      #else
  153.      asm  mov     di,buff
  154.      #endif
  155.      asm  mov     cx,80*25  
  156.      
  157.      wait_hi:
  158.      asm  in      al,dx 
  159.      asm  test    al,1
  160.      asm  jnz     wait_hi
  161.      asm   cli 
  162.      
  163.      wait_lo:
  164.      asm  in      al,dx 
  165.      asm  test    al,1
  166.      asm  jz      wait_lo
  167.      asm  movsw   
  168.      asm  sti
  169.      asm  loop    wait_hi
  170.      
  171.      asm  pop     es
  172.      asm  pop     ds
  173. }
  174.  
  175.  
  176. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  177. /*  ║ Function :  dma_ca();                                                ║  */
  178. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  179. /*  ║ put character and attribute to screen at a column/row using  direct  ║  */
  180. /*  ║ screen writes w/snowcheck                                            ║  */
  181. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  182. /*  ║ Usage    : dma_ca(col,row,attr,cha);                                 ║  */
  183. /*  ║                                                                      ║  */
  184. /*  ║ Returns  : nothing                                                   ║  */
  185. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  186. void dma_ca(col,row,attr,cha)
  187. int col,row,attr,cha;
  188. {
  189.      int buff;
  190.      buff = row * 80 + col;
  191.      
  192.      asm     push    ds
  193.      
  194.      
  195.      asm     mov     ah,15
  196.      asm     int     10h
  197.      asm     mov     dx,3BAh
  198.      asm     mov     di,buff
  199.      asm     add     di,di
  200.      asm     cmp     al,7
  201.      asm     je      movmno
  202.      asm     mov     dx,3DAh
  203.      asm     mov     ax,0B800h
  204.      asm     jmp     Video
  205.      
  206.      movmno:
  207.      asm     mov     ax,0B000h
  208.      asm     mov     es,ax
  209.      asm     mov     cx,1
  210.      asm     mov     al,cha
  211.      asm     mov     es:[di],al
  212.      asm     add     di,1
  213.      asm     mov     al,attr
  214.      asm     mov     es:[di],al
  215.      asm     jmp     exit
  216.      
  217.      Video:     
  218.      asm     mov     es,ax
  219.      asm     mov     cx,1
  220.      
  221.      wait_hi:
  222.      asm     in      al,dx
  223.      asm     test    al,1
  224.      asm     jnz     wait_hi
  225.      
  226.      wait_lo: 
  227.      asm     in      al,dx
  228.      asm     test    al,1
  229.      asm     jz      wait_lo
  230.      asm     mov     al,cha
  231.      asm     mov     es:[di],al
  232.      
  233.      asm     add     di,1
  234.      
  235.      wait_h2:
  236.      asm     in      al,dx
  237.      asm     test    al,1
  238.      asm     jnz     wait_h2
  239.      
  240.      wait_l2: 
  241.      asm     in      al,dx
  242.      asm     test    al,1
  243.      asm     jz      wait_l2
  244.      asm     mov     al,attr
  245.      asm     mov     es:[di],al
  246.      
  247.      exit:
  248.      asm     pop    ds
  249. }
  250.  
  251.  
  252. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  253. /*  ║ Function :  putscrn();                                               ║  */
  254. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  255. /*  ║ puts memory buffer to screen using direct screen writes w/snowcheck  ║  */
  256. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  257. /*  ║ Usage    : putscrn(buff);                                            ║  */
  258. /*  ║                                                                      ║  */
  259. /*  ║ Returns  : nothing                                                   ║  */
  260. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  261. /*  ║      ex:  static char  buff[4000];                                   ║  */
  262. /*  ║                                                                      ║  */
  263. /*  ║            getscrn(buff);    SAVE SCREEN                             ║  */
  264. /*  ║            other_stuff();    DO SOME STUFF                           ║  */
  265. /*  ║            putscrn(buff);    RESTORE SCREEN                          ║  */
  266. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  267. void putscrn(buff)
  268. char far *buff;
  269. {
  270.      asm    mov     ah,15            
  271.      asm    int     10h              
  272.      asm    mov     dx,3BAh          
  273.      asm    mov     di,0             
  274.      asm    cmp     al,7             
  275.      asm    je      movscn           
  276.      asm    mov     dx,3DAh          
  277.      asm    mov     di,8000h         
  278.      movscn: 
  279.      asm    mov     ax,0B000h
  280.      asm    mov     es,ax
  281.      
  282.      #if LARGE
  283.      asm    push    ds
  284.      asm    lds     si,buff
  285.      #else
  286.      asm    mov     si,buff
  287.      #endif
  288.      asm    mov     cx,80*25         
  289.      
  290.      wait_hi:
  291.      asm    in      al,dx            
  292.      asm    test    al,1             
  293.      asm    jnz     wait_hi           
  294.      wait_lo:
  295.      asm    in      al,dx            
  296.      asm    test    al,1             
  297.      asm    jz      wait_lo           
  298.      asm    movsw                    
  299.      
  300.      asm    loop    wait_hi           
  301.      
  302.      #if LARGE
  303.      asm    pop     ds
  304.      #endif
  305. }
  306.  
  307.  
  308. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  309. /*  ║ Function :  dma_str();                                               ║  */
  310. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  311. /*  ║ writes string and attribute direct to screen w/snowcheck             ║  */
  312. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  313. /*  ║ Usage    : dma_str(string,attr);                                     ║  */
  314. /*  ║                                                                      ║  */
  315. /*  ║ Returns  : nothing                                                   ║  */
  316. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  317. /*  ║   requires: fastwrite();                                             ║  */
  318. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  319. void dma_str(char *st, int attr)
  320. {
  321.      int row,col,x1;
  322.      row = where_y();
  323.      col = where_x();
  324.      x1 = col + strlen(st);
  325.      fastwrite(col,row,attr,st);
  326.      if(x1 > 79)   {
  327.           col = x1 - 80;
  328.           row++;
  329.      }
  330.      else
  331.      col = x1;
  332.      if(row > 24)   {
  333.           scroll(0,0,79,24,1,0);
  334.           row = 24;
  335.      }
  336.      gotoxy(col,row);
  337. }
  338.  
  339.  
  340. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  341. /*  ║ Function :  scn_attr();                                              ║  */
  342. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  343. /*  ║ writes attributes only direct to screen with CGA snowcheck           ║  */
  344. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  345. /*  ║ Usage    : scn_attr(attr,col,row,len);                               ║  */
  346. /*  ║                                                                      ║  */
  347. /*  ║ Returns  : nothing                                                   ║  */
  348. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  349. void scn_attr(attr,col,row,len)
  350. int attr,col,row,len;
  351. {
  352.      int buff;
  353.      buff = row * 80 + col;
  354.      
  355.      asm     push    ds
  356.      
  357.      
  358.      asm     mov     ah,15
  359.      asm     int     10h
  360.      asm     mov     dx,3BAh
  361.      asm     mov     di,buff
  362.      asm     add     di,di
  363.      asm     cmp     al,7
  364.      asm     je      movmno
  365.      asm     mov     dx,3DAh
  366.      asm     mov     ax,0B800h
  367.      asm     jmp     Video
  368.      
  369.      movmno:
  370.      asm     mov     ax,0B000h
  371.      Video:     
  372.      asm     add     di,1
  373.      asm     mov     es,ax
  374.      asm     mov     cx,len
  375.      
  376.      wait_hi:
  377.      asm     in      al,dx
  378.      asm     test    al,1
  379.      asm     jnz     wait_hi
  380.      
  381.      wait_lo: 
  382.      asm     in      al,dx
  383.      asm     test    al,1
  384.      asm     jz      wait_lo
  385.      asm     mov     ah,attr
  386.      asm     mov     es:[di],ah
  387.      asm     add     di,2
  388.      asm     loop    wait_hi
  389.      
  390.      asm     pop    ds
  391. }
  392.  
  393.  
  394. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  395. /*  ║ Function :  set_drive();                                             ║  */
  396. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  397. /*  ║ see if drive is available, and if so, change to it                   ║  */
  398. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  399. /*  ║ Usage    : set_drive(num);                                           ║  */
  400. /*  ║                      num ->  0=A, 1=B, 2=C, 3=D, etc.                ║  */
  401. /*  ║                                                                      ║  */
  402. /*  ║ Returns  : nothing                                                   ║  */
  403. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  404.  
  405. void set_drive(drv_num)
  406. int drv_num;
  407. {
  408.      int new_drv,cur_drv;
  409.      asm     mov     ah,19h        /* get current drive   */
  410.      asm     int     21h                                    
  411.      asm     mov     cur_drv,al    /* save it to cur_drv  */
  412.      asm     mov     ah,0eh        /* select disk drive   */
  413.      asm     mov     dl,drv_num                             
  414.      asm     int     21h                                    
  415.      asm     mov     ah,19h        /* get the drive       */
  416.      asm     int     21h                                    
  417.      asm     cmp     al,drv_num    /* compare it          */
  418.      asm     je      n_drive                                
  419.      asm     mov     al,cur_drv    /* not there so make   */
  420.      asm     mov     drv_num,al    /* drv_num the cur_drv */
  421.      n_drive:                                                      
  422.      asm     mov     ah,0eh                                 
  423.      asm     mov     dl,drv_num    /* set to drv_num      */
  424.      asm     int     21h
  425. }
  426.  
  427.  
  428. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  429. /*  ║ Function :  vpeek();                                                 ║  */
  430. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  431. /*  ║ read a character and attribute int video ram with CGA snowcheck      ║  */
  432. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  433. /*  ║ Usage    : vpeek(vid_seg,offset);                                    ║  */
  434. /*  ║                                                                      ║  */
  435. /*  ║ Returns  : character at vid_seg,offset                               ║  */
  436. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  437. int vpeek(unsigned vseg, unsigned adr)
  438. {
  439.      int ch, at;
  440.      
  441.      if (vseg == 45056)          /* monochrome mode */
  442.      return peek(vseg, adr);
  443.      asm push ds;
  444.      _DX = 986;       /* video status port */
  445.      _DS = vseg;      /* video segment address */
  446.      _SI = adr;       /* video character offset */
  447.      asm cld;
  448.      /* -------- wait for video retrace to start -------- */
  449.      do
  450.      asm in  al,dx;
  451.      while (_AL & 1);
  452.      /* -------- wait for video retrace to stop -------- */
  453.      do
  454.      asm in  al,dx;
  455.      while (!(_AL & 1));
  456.      asm lodsb;         /* get the character */
  457.      _BL = _AL;
  458.      /* -------- wait for video retrace to start -------- */
  459.      do
  460.      asm in  al,dx;
  461.      while (_AL & 1);
  462.      /* -------- wait for video retrace to stop -------- */
  463.      do
  464.      asm in  al,dx;
  465.      while (!(_AL & 1));
  466.      asm lodsb;       /* get the attribute */
  467.      _BH = _AL;
  468.      _AX = _BX;
  469.      asm pop ds;
  470.      return _AX;
  471. }
  472.  
  473.  
  474. /*  ╔══════════════════════════════════════════════════════════════════════╗  */
  475. /*  ║ Function :  vpoke();                                                 ║  */
  476. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  477. /*  ║ insert a character and attribute int video ram with CGA snowcheck    ║  */
  478. /*  ╠══════════════════════════════════════════════════════════════════════╣  */
  479. /*  ║ Usage    : vpoke(vid_seg,offset,chr);                                ║  */
  480. /*  ║                                                                      ║  */
  481. /*  ║ Returns  : nothing                                                   ║  */
  482. /*  ╚══════════════════════════════════════════════════════════════════════╝  */
  483. void vpoke(unsigned vseg, unsigned adr, unsigned chr)
  484. {
  485.      if (vseg == 45056)             /* monochrome mode */
  486.      poke(vseg, adr, chr);
  487.      else   {
  488.           _DI = adr;      /* offset of video character */
  489.           _ES = vseg;     /* video segment */
  490.           asm cld;
  491.           _BX = chr;      /* the attribute and character */
  492.           _DX = 986;      /* video status port */
  493.           /* -------- wait for video retrace to start -------- */
  494.           do
  495.           asm in  al,dx;
  496.           while (_AL & 1);
  497.           /* -------- wait for video retrace to stop -------- */
  498.           do
  499.           asm in  al,dx;
  500.           while (!(_AL & 1));
  501.           _AL = _BL;
  502.           asm stosb;      /* store character */
  503.           /* -------- wait for video retrace to start -------- */
  504.           do
  505.           asm in  al,dx;
  506.           while (_AL & 1);
  507.           /* -------- wait for video retrace to stop -------- */
  508.           do
  509.           asm in  al,dx;
  510.           while (!(_AL & 1));
  511.           _AL = _BH;
  512.           asm stosb;     /* store attribute */
  513.      }
  514. }
  515.  
  516.  
  517.